package com.game.thrones.controller.webpage;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import com.game.thrones.service.web.WebEndemicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.game.thrones.constant.Errors;
import com.game.thrones.controller.BaseController;
import com.game.thrones.controller.input.DataInput;
import com.game.thrones.controller.input.PageInput;
import com.game.thrones.controller.output.BaseOutput;
import com.game.thrones.controller.output.PageOutput;
import com.game.thrones.controller.output.ResultResponseBeanUtil;
import com.game.thrones.controller.output.SelectOutput;
import com.game.thrones.domain.KbmContent;
import com.game.thrones.domain.webpage.WebCourseDO;
import com.game.thrones.exception.GameGlobalException;
import com.game.thrones.service.web.WebCourseService;

//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;

//@Api(value = "网站-课程信息", description = "网站-课程信息-田春阳", produces = "application/json")
@Controller
public class WebCourseController extends BaseController {
	private Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	WebCourseService webCourseService;
	@Autowired
	WebEndemicService webEndemicService;
//	@ApiOperation(value = "分页列表", notes = "分页列表")
	@ResponseBody
	@RequestMapping(value = "/web/webCourse/listByPage", method = RequestMethod.POST)
	public PageOutput<WebCourseDO> listByPage(HttpServletRequest request, @RequestBody PageInput input) {
		KbmContent kbmContent = getCurrentInfo(request);
		input.setKbmContent(kbmContent);
		List<WebCourseDO> entityList = webCourseService.findByPage(input);
		Integer total = webCourseService.countByPage(input);
		return ResultResponseBeanUtil.pageSuccess(entityList, input.getSize(), input.getPage(), total);
	}

//	@ApiOperation(value = "列表(前端页面用)", notes = "列表(前端页面用)")
	@ResponseBody
	@RequestMapping(value = "/web/webCourse/list", method = RequestMethod.POST)
	public BaseOutput<List<WebCourseDO>> list(HttpServletRequest request ) {
		KbmContent kbmContent = webEndemicService.findKbmContent(request);
		List<WebCourseDO> entityList = webCourseService.findAll(kbmContent);
		return ResultResponseBeanUtil.success(entityList);
	}
//	@ApiOperation(value = "下拉数据", notes = "下拉数据")
	@ResponseBody
	@RequestMapping(value = "/web/webCourse/dropdown", method = RequestMethod.POST)
	public BaseOutput<List<SelectOutput>> dropdown(HttpServletRequest request ) {
		KbmContent kbmContent = getCurrentInfo(request);
		List<WebCourseDO> entityList = webCourseService.findAll(kbmContent);
		List<SelectOutput> outputList = new ArrayList<SelectOutput>();
		for(WebCourseDO entity:entityList){
			SelectOutput output = new SelectOutput();
			output.setId(entity.getId());
			output.setName(entity.getName());
			outputList.add(output);
		}
		return ResultResponseBeanUtil.success(outputList);
	}
//	@ApiOperation(value = "新增", notes = "新增")
	@ResponseBody
	@RequestMapping(value = "/web/webCourse/add", method = RequestMethod.POST)
	public BaseOutput<Void> add(HttpServletRequest request, @Valid @RequestBody WebCourseDO input) {
		KbmContent kbmContent = getCurrentInfo(request);
		if (kbmContent == null) {
			throw new GameGlobalException(Errors.MODIFY_ERROR);
		}
		input.setKbmContent(kbmContent);
		webCourseService.add(input);
		return ResultResponseBeanUtil.success();
	}

//	@ApiOperation(value = "修改", notes = "修改")
	@ResponseBody
	@RequestMapping(value = "/web/webCourse/update", method = RequestMethod.POST)
	public BaseOutput<Void> update(HttpServletRequest request, @Valid @RequestBody WebCourseDO input) {
		KbmContent kbmContent = getCurrentInfo(request);
		if (kbmContent == null) {
			throw new GameGlobalException(Errors.MODIFY_ERROR);
		}
		input.setKbmContent(kbmContent);
		webCourseService.update(input);
		return ResultResponseBeanUtil.success();
	}

//	@ApiOperation(value = "删除", notes = "删除")
	@ResponseBody
	@RequestMapping(value = "/web/webCourse/delete", method = RequestMethod.POST)
	public BaseOutput<Void> delete(HttpServletRequest request, @Valid @RequestBody DataInput input) {
		KbmContent kbmContent = getCurrentInfo(request);
		WebCourseDO entity = webCourseService.findOneById(kbmContent, input.getDataId());
		if (entity == null) {
			throw new GameGlobalException(Errors.DATA_NOTEXIST);
		}
		entity.setKbmContent(kbmContent);
		webCourseService.del(entity);
		return ResultResponseBeanUtil.success();
	}
	
//	@ApiOperation(value = "根据ID获取", notes = "根据ID获取")
	@ResponseBody
	@RequestMapping(value = "/web/webCourse/findOneById", method = RequestMethod.POST)
	public BaseOutput<WebCourseDO> findOneById(HttpServletRequest request, @Valid @RequestBody DataInput input) {
		KbmContent kbmContent = getCurrentInfo(request);
		WebCourseDO entity = webCourseService.findOneById(kbmContent, input.getDataId());
		return ResultResponseBeanUtil.success(entity);
	}
}
